﻿function CheckHelper(){
    //初始化绑定事件对象
    var initObj = null;
    //name为指定的子集合
    var checkDoms = null;     
    var ary = new Array();

    //获取初始化全选的状态
    var setupInit = function () {
        var allCheckedStatus = $(initObj).prop("checked"); //get chekced state
        $(checkDoms).prop("checked", allCheckedStatus);
    };

    //绑定全选对象事件
    var bindCheckEvent = function(obj) {
        $(obj).on("click", function() {
            setupInit();
        });
    };

    //初始化操作
    this.init = function (elementId) {
        $(function() {
            initObj = $("input[type='checkbox'][id='" + elementId + "']"); //obj;
            if (initObj[0]) {
                var name = $(initObj).attr("name");
                checkDoms = $("input[type='checkbox'][name='" + name + "']");
                //bind all checkbox event
                bindCheckEvent(initObj);
            }
        });
    };

    //默认获取选中值 取Value
    var getCheckValue = function (element) {
        return $.trim($(element).val()) || null;
    };

    //获取选中数据的值集合  默认为取value值
    //dymincFunction 返回input checkbox 对象要进一步分析
    this.values = function (dymincFunction) {
        if (initObj==null||!initObj[0]) {
            return null;
        }
        //初始化数组
        ary.length = 0;
        //去除绑定事件对象
        var queryCheckValueDom = $.grep(checkDoms, function(n, i) {
            return n != initObj[0];
        });
        $(queryCheckValueDom).each(function(index, element) {
            if ($(element).prop("checked") == true) {
                var vl = getCheckValue(element);
                try {
                    if (dymincFunction != null && dymincFunction.constructor == Function) {
                        vl = dymincFunction($(element));
                    }
                } catch (e) {
                    vl = null;
                    console.log(e);
                }
                if (vl != null)
                    ary.push(vl);
            }
        });
        return ary.length > 0 ? ary : null;
    };

    this.setDisable = function () {
        $(checkDoms).prop("disabled", "disabled");
    };

    this.removeDisable = function () {
        $(checkDoms).prop("disabled", false);
    };
};